package net.bw.realtime.jtp.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.bw.realtime.jtp.bean.*;
import org.bw.realtime.jtp.service.TradeOrderService;
import org.bw.realtime.jtp.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: lianyuez
 * @Date: 2025/6/5
 */

@RestController
@RequestMapping("/trade")
public class TradeOrderController {

	private static final String DATE_DEFAULT_VALUE = "2025-01-25";

	@Autowired
	private TradeOrderService tradeOrderService;

//	todo 总下单量订单数
	@RequestMapping("/getTotalOrderCnt")
	public String getTotalOrderCnt(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE)String date) {

		// 1.获取参数
		if(DATE_DEFAULT_VALUE.equals(date)){
			date = DateUtil.nowDate();
		}

		// 2.调用service
		Double totalOrderCnt = tradeOrderService.getTotalOrderCnt(date);

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("status",0);
		jsonObject.put("msg","");
		jsonObject.put("data",totalOrderCnt);


		return jsonObject.toJSONString();
	}

//	todo 总下单量下单金额
	@RequestMapping("/getTotalAmount")
	public String getTotalAmount(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE)String date) {

		// 1.获取参数
		if(DATE_DEFAULT_VALUE.equals(date)){
			date = DateUtil.nowDate();
		}

		// 2.调用service
		Double totalAmount = tradeOrderService.getTotalAmount(date);

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("status",0);
		jsonObject.put("msg","");
		jsonObject.put("data",totalAmount);


		return jsonObject.toJSONString();
	}

	// --  todo 省份

//	todo 各个省份下单金额
	@RequestMapping("/getProvinceAmount")
	public String getProvinceAmount(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE)String date) {

		// 1.获取参数
		if(DATE_DEFAULT_VALUE.equals(date)){
			date = DateUtil.nowDate();
		}

		// 2.调用service
		List<ProvinceAmountBean> provinceAmount = tradeOrderService.getProvinceAmount(date);

		JSONObject object1 = new JSONObject();
		object1.put("status",0);
		object1.put("msg","");
		JSONObject object2 = new JSONObject();
		object1.put("data",object2);

		JSONArray array1 = new JSONArray();

		for (ProvinceAmountBean hotSkuBean : provinceAmount) {
			JSONObject object3 = new JSONObject();
			object3.put("name",hotSkuBean.getProvince_name());
			object3.put("value",hotSkuBean.getOrder_amount());
			object3.put("sizeValue",659);
			object3.put("type",3);
			JSONArray array2 = new JSONArray();
			array2.add(78);
			array2.add(5);
			object3.put("tooltipValues",array2);
			array1.add(object3);
		}
		object2.put("mapData",array1);
		object2.put("valueName","销售额");
		object2.put("sizeValueName","GDP");
		object2.put("typeName","级别");
		JSONArray array3 = new JSONArray();
		array3.add("面积");
		array3.add("流动人口");
		object2.put("tooltipNames",array3);
		JSONArray array4 = new JSONArray();
		array4.add("平方公里");
		array4.add("人");
		object2.put("tooltipUnits",array4);

		return object1.toJSONString();

	}

//	todo 各个省份下单量排名
	@RequestMapping("/getProvinceCntRank")
	public String getProvinceCntRank(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE)String date) {

		// 1.获取参数
		if(DATE_DEFAULT_VALUE.equals(date)){
			date = DateUtil.nowDate();
		}

		// 2.调用service
		List<ProvinceCntRankBean> provinceCntRank = tradeOrderService.getProvinceCntRank(date);

		JSONObject object1 = new JSONObject();
		object1.put("status",0);
		object1.put("msg","");
		JSONObject object2 = new JSONObject();
		object1.put("data",object2);

		JSONArray array1 = new JSONArray();

		JSONArray array2 = new JSONArray();

		JSONObject object3 = new JSONObject();

		JSONArray array4 = new JSONArray();

		for (ProvinceCntRankBean provinceCntRankBean : provinceCntRank) {

			array1.add(provinceCntRankBean.getProvince_name());
			array4.add(provinceCntRankBean.getOrder_count());
		}
		object3.put("name","下单量");
		object3.put("data",array4);

		array2.add(object3);

		object2.put("categories",array1);
		object2.put("series",array2);

		return object1.toJSONString();

	}

//	todo 各个省份销售量量排名
	@RequestMapping("/getProvinceAmountRank")
	public String getProvinceAmountRank(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE)String date) {
		// 1.获取参数
		if(DATE_DEFAULT_VALUE.equals(date)){
			date = DateUtil.nowDate();
		}

		// 2.调用service
		List<ProvinceAmountRankBean> provinceAmountRank = tradeOrderService.getProvinceAmountRank(date);

		JSONObject object1 = new JSONObject();
		object1.put("status",0);
		object1.put("msg","");
		JSONObject object2 = new JSONObject();
		object1.put("data",object2);

		JSONArray array1 = new JSONArray();
		JSONArray array2 = new JSONArray();

		JSONObject object3 = new JSONObject();
		JSONObject object4 = new JSONObject();

		object3.put("name","2025-01-25");
		object4.put("name","2025-06-01");

		JSONArray array3 = new JSONArray();
		JSONArray array4 = new JSONArray();

		for (ProvinceAmountRankBean provinceCntRankBean : provinceAmountRank) {

			array1.add(provinceCntRankBean.getProvince_name());
			array3.add(provinceCntRankBean.getOne_month_amount());
			array4.add(provinceCntRankBean.getSix_month_amount());
		}

		object3.put("data",array3);
		object4.put("data",array4);

		array2.add(object3);
		array2.add(object4);

		object2.put("categories",array1);
		object2.put("series",array2);

		return object1.toJSONString();


	}

//	-- todo 来源

//	todo 每日订单不同来源下单量、下单金额

	@RequestMapping("/getSourceAmount")
	public String getSourceAmount(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE)String date) {

		// 1.获取参数
		if(DATE_DEFAULT_VALUE.equals(date)){
			date = DateUtil.nowDate();
		}

		// 2.调用service
		List<SourceAmountBean> sourceAmount = tradeOrderService.getSourceAmount(date);

		JSONObject object1 = new JSONObject();
		object1.put("status",0);
		object1.put("msg","");

		JSONArray array1 = new JSONArray();

		for (SourceAmountBean sourceAmountBean : sourceAmount) {
			JSONObject object3 = new JSONObject();
			object3.put("name",sourceAmountBean.getSource_type_name());
			object3.put("value",sourceAmountBean.getOrder_amount());
			array1.add(object3);
		}

		object1.put("data",array1);

		return object1.toJSONString();

	}

//	-- todo sku

//	todo 每日订单销售品牌排名 (销售金额)
	@RequestMapping("/getSkuAmount")
	public String getSkuAmount(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE)String date) {

		// 1.获取参数
		if(DATE_DEFAULT_VALUE.equals(date)){
			date = DateUtil.nowDate();
		}

		// 2.调用service
		List<SkuAmountBean> skuAmount = tradeOrderService.getSkuAmount(date);

		JSONObject object1 = new JSONObject();
		object1.put("status",0);
		object1.put("msg","");
		JSONObject object2 = new JSONObject();
		object1.put("data",object2);

		JSONArray array1 = new JSONArray();

		JSONArray array2 = new JSONArray();

		JSONObject object3 = new JSONObject();

		JSONArray array4 = new JSONArray();

		for (SkuAmountBean skuAmountBean : skuAmount) {
			array1.add(skuAmountBean.getSku_name());
			array4.add(skuAmountBean.getOrder_amount());
		}
		object3.put("name","");
		object3.put("data",array4);

		array2.add(object3);

		object2.put("categories",array1);
		object2.put("series",array2);

		return object1.toJSONString();

	}

	//	todo 每日订单不同品牌分布 （商品销量）


	@RequestMapping("/getSkuCnt")
	public String getSkuCnt(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE)String date) {

		// 1.获取参数
		if(DATE_DEFAULT_VALUE.equals(date)){
			date = DateUtil.nowDate();
		}

		// 2.调用service
		List<SkuCntBean> skuAmount = tradeOrderService.getSkuCnt(date);

		JSONObject object1 = new JSONObject();
		object1.put("status",0);
		object1.put("msg","");

		JSONObject object2 = new JSONObject();
		object1.put("data",object2);


		JSONArray array1 = new JSONArray();

		JSONArray array2 = new JSONArray();

		JSONObject object3 = new JSONObject();

		JSONArray array4 = new JSONArray();

		for (SkuCntBean skuAmountBean : skuAmount) {
			array1.add(skuAmountBean.getSku_name());
			array4.add(skuAmountBean.getOrder_count());
		}
		object3.put("name","品牌");
		object3.put("data",array4);

		array2.add(object3);

		object2.put("categories",array1);
		object2.put("series",array2);


		return object1.toJSONString();

	}


}
